package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.PayApp;
import org.summer.lawyer.domain.bo.payapp.PayAppBo;
import org.summer.lawyer.domain.vo.payapp.PayAppVo;
import org.summer.lawyer.mapper.PayAppMapper;
import org.summer.lawyer.service.IPayAppService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 支付应用Service业务层处理
 *
 * @author summer
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class PayAppServiceImpl extends ServiceImpl<PayAppMapper, PayApp>  implements IPayAppService {


    /**
     * 查询支付应用
     *
     * @param appId 主键
     * @return 支付应用
     */
    @Override
    public PayAppVo selectPayAppVoById(Long appId){
        return baseMapper.selectVoById(appId);
    }

    /**
     * 分页查询支付应用列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 支付应用分页列表
     */
    @Override
    public TableDataInfo<PayAppVo> selectPageList(PayAppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayApp> lqw = buildQueryWrapper(bo);
        Page<PayAppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的支付应用列表
     *
     * @param bo 查询条件
     * @return 支付应用列表
     */
    @Override
    public List<PayAppVo> selectPayAppVoList(PayAppBo bo) {
        LambdaQueryWrapper<PayApp> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayApp> buildQueryWrapper(PayAppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayApp> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getAppName()), PayApp::getAppName, bo.getAppName());
        lqw.eq(StrUtil.isNotBlank(bo.getPayNotifyUrl()), PayApp::getPayNotifyUrl, bo.getPayNotifyUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getRefundNotifyUrl()), PayApp::getRefundNotifyUrl, bo.getRefundNotifyUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getTransferNotifyUrl()), PayApp::getTransferNotifyUrl, bo.getTransferNotifyUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getMchNo()), PayApp::getMchNo, bo.getMchNo());
        lqw.like(StrUtil.isNotBlank(bo.getMchName()), PayApp::getMchName, bo.getMchName());
        lqw.eq(StrUtil.isNotBlank(bo.getAppSecret()), PayApp::getAppSecret, bo.getAppSecret());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), PayApp::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增支付应用
     *
     * @param bo 支付应用
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayAppBo bo) {
        PayApp add = MapstructUtils.convert(bo, PayApp.class);
        validEntityBeforeSave(add);
        add.setStatus(SystemConstants.ACTIVE);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppId(add.getAppId());
        }
        return flag;
    }

    /**
     * 修改支付应用
     *
     * @param bo 支付应用
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayAppBo bo) {
        PayApp update = MapstructUtils.convert(bo, PayApp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayApp entity){
        PayAppBo bo = new PayAppBo();
        bo.setAppId(entity.getAppId());
        bo.setAppName(entity.getAppName());
        if(checkUniqueAppName(bo)){
            throw new ServiceException(StrUtil.format("“{}”应用名称已经存在，请重新输入", entity.getAppName()));
        }
    }

    /**
     * 校验并批量删除支付应用信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean checkUniqueAppName(PayAppBo bo) {
        return baseMapper.exists(new LambdaQueryWrapper<PayApp>().eq(PayApp::getAppName, bo.getAppName()).ne(bo.getAppId() != null,PayApp::getAppId, bo.getAppId()));
    }

    @Override
    public void changePayAppStatus(PayAppBo bo) {
        LambdaUpdateWrapper<PayApp> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(PayApp::getAppId, bo.getAppId())
                .set(PayApp::getStatus, bo.getStatus());
        baseMapper.update(updateWrapper);
    }

    @Override
    public PayAppVo selectPayAppVoByDefault() {
        List<PayAppVo> appVoList = baseMapper.selectVoList();
        if(CollUtil.isNotEmpty(appVoList)){
            return appVoList.getFirst();
        }
        return null;
    }
}
